package org.sws.domain.award.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.sws.domain.award.aggregate.AwardRecordTaskAggregate;
import org.sws.domain.award.event.SendAwardMessageEvent;
import org.sws.domain.award.model.entity.AwardRecordEntity;
import org.sws.domain.award.model.entity.DistributeAwardEntity;
import org.sws.domain.award.model.entity.TaskEntity;
import org.sws.domain.award.repository.IAwardRepository;
import org.sws.domain.award.service.IAwardService;
import org.sws.domain.award.service.IDistributeAwardService;
import org.sws.types.enums.TaskState;
import org.sws.types.event.BaseEvent;

import java.util.List;
import java.util.Map;

/**
 * @Author sws
 * @Date 2025/3/24 11:50
 * @description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AwardService implements IAwardService {

    private final IAwardRepository awardRepository;
    private final SendAwardMessageEvent sendAwardMessageEvent;
    private final Map<String, IDistributeAwardService> distributeAwardServiceMap;
    @Override
    public void createAwardRecord(AwardRecordEntity awardRecordEntity) {
        // 构建MQ消息对象
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage = SendAwardMessageEvent.SendAwardMessage.builder()
                .userId(awardRecordEntity.getUserId())
                .orderId(awardRecordEntity.getOrderId())
                .awardId(awardRecordEntity.getAwardId())
                .awardTitle(awardRecordEntity.getAwardTitle())
                .awardConfig(awardRecordEntity.getAwardConfig())
                .build();
        BaseEvent.EventMessage<SendAwardMessageEvent.SendAwardMessage> sendAwardMessageEventMessage = sendAwardMessageEvent.buildEventMessage(sendAwardMessage);
        // 构建Task对象
        TaskEntity taskEntity = TaskEntity.builder()
                .userId(awardRecordEntity.getUserId())
                .topic(sendAwardMessageEvent.topic())
                .messageId(sendAwardMessageEventMessage.getId())
                .message(sendAwardMessageEventMessage)
                .state(TaskState.create)
                .build();
        // 构建AwardRecordTaskAggregate聚合对象
        AwardRecordTaskAggregate awardRecordTaskAggregate = AwardRecordTaskAggregate.builder()
                .userAwardRecordEntity(awardRecordEntity)
                .taskEntity(taskEntity)
                .build();
        // 保存记录
        awardRepository.createAwardRecord(awardRecordTaskAggregate);
    }
    @Override
    public List<AwardRecordEntity> queryAwardRecord(String userId, Long activityId) {
        return awardRepository.queryAwardRecord(userId, activityId);
    }

    @Override
    public void distributeAward(DistributeAwardEntity distributeAwardEntity) {
        // 奖品Key
        String awardKey = awardRepository.queryAwardKey(distributeAwardEntity.getAwardId());
        if (null == awardKey) {
            log.error("分发奖品，奖品ID不存在。awardKey:{}", awardKey);
            return;
        }

        // 奖品服务
        IDistributeAwardService distributeAward = distributeAwardServiceMap.get(awardKey);

        if (null == distributeAward) {
            log.error("分发奖品，对应的服务没对接。awardKey:{}", awardKey);
//            throw new RuntimeException("分发奖品，奖品" + awardKey + "对应的服务不存在");
            return;
        }

        // 不同发放奖品动作
        distributeAward.giveOutPrizes(distributeAwardEntity);
    }
}
